# 十四届2023年复习
import math
from collections import deque
from datetime import datetime


def shortest_time(matrix: list[list[int]]):
    """走方格"""
    n = len(matrix)
    INF = float('inf')
    dist = [[INF] * n for _ in range(n)]
    dist[0][0] = 0
    que = deque([(0.0, 0, 0)])  # 起始节点(路径长度, r, c)
    while que:
        l, r, c = que.popleft()
        if l > dist[r][c]:
            continue

        if r == n - 1 and c == n - 1:
            return l

        if r < n - 1:  # 向下移动一格
            if dist[r + 1][c] > dist[r][c] + 1:
                dist[r + 1][c] = dist[r][c] + 1
                que.append((dist[r + 1][c], r + 1, c))

        if c < n - 1:  # 向右移动一格
            if dist[r][c + 1] > dist[r][c] + 1:
                dist[r][c + 1] = dist[r][c] + 1
                que.append((dist[r][c + 1], r, c + 1))

        cl = c  # 向右移动L格
        while cl < n - 1 and matrix[r][cl] < matrix[r][cl + 1]:
            cl += 1
        if dist[r][cl] > dist[r][c] + 1:
            dist[r][cl] = dist[r][c] + 1
            que.append((dist[r][cl], r, cl))

    return dist[n - 1][n - 1]


def count_prime_x(num: int):
    def sieve(n: int):
        is_prime = [True] * (n + 1)
        is_prime[0] = is_prime[1] = False
        for i in range(2, int(math.sqrt(n)) + 1):
            if is_prime[i]:
                for j in range(i * i, n + 1, i):
                    is_prime[j] = False
        return set(prime for prime, is_it in enumerate(is_prime) if is_it)

    def is_x_prime(n: int):
        nonlocal primes, base_primes
        digits = str(n)
        if any(int(d) in base_primes for d in digits):
            return True

        sub_seqs = set()
        for d in digits:
            seqs = {sub + d for sub in sub_seqs}
            if any(int(seq) in primes for seq in seqs):
                return True
            sub_seqs.update(seqs)
            sub_seqs.add(d)

    cnt = 0
    primes = sieve(num)
    base_primes = [2, 3, 5, 7]
    for i in range(1, num + 1):
        if is_x_prime(i):
            cnt += 1
    return cnt


def count_minutes(num: int):
    # 整数变换
    cnt = 0
    while num != 0:
        cnt += 1
        digits = str(num)
        for d in digits:
            num -= int(d)
    return cnt


def count_work_time():
    file = "../data/2023_a.txt"
    formattime = "%Y-%m-%d %H:%M:%S"
    l = []
    with open(file, "r") as file:
        line = file.readline()[:-1]
        while line and line != "\n":
            dt = datetime.strptime(line, formattime)
            l.append(dt)
            line = file.readline()[:-1]
    l.sort(reverse=True)
    n = len(l)
    res = 0
    for i in range(n // 2):
        res += l[i * 2].timestamp() - l[i * 2 + 1].timestamp()
    return res


if __name__ == '__main__':
    print(count_work_time())
